﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using PipeTest.Game;
using PipeTest.Script;
using PipeTest.Win32;

namespace PipeTest
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        private static void MainFormFormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void SearchButtonClick(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("ge");
            ProcComboBox.DisplayMember = "MainWindowTitle";
            ProcComboBox.ValueMember = "Id";
            ProcComboBox.DataSource = processes;
        }

        ScriptManager scriptManager = new ScriptManager();
        private GameVistor gameVistor;

        private void ExecuteButtonClick(object sender, EventArgs e)
        {
            if (gameVistor == null) gameVistor = new GameVistor((int) ProcComboBox.SelectedValue);

            scriptManager.ExecuteScript(ScriptTextBox.Text, gameVistor);
        }

        private void StopButtonClick(object sender, EventArgs e)
        {
            scriptManager.Stop();
        }

        private void Mode3Timer_Tick(string key)
        {
            foreach (var e in key)
            {
                NativeMethod.keybd_event((byte) e, (byte) NativeMethod.MapVirtualKey(e, 0), NativeMethod.KEYEVENTF_EXTENDEDKEY, 0);
                Thread.Sleep(50);
                NativeMethod.keybd_event((byte) e, (byte) NativeMethod.MapVirtualKey(e, 0), NativeMethod.KEYEVENTF_EXTENDEDKEY|NativeMethod.KEYEVENTF_KEYUP, 0);
            }
        }

    }
}